#!/bin/bash
export LITOS_PROJECT_DIR=$(pwd)
export LITOS_APP="bootloader"

function select_chip()
{
	local cnt=0
	local choice
	local board_list

	printf "All available chips:\n"
	for boarddir in `ls -F bsp/ |grep "/$"` ; do
		chips[$cnt]=`basename $boarddir`
		printf "%4d. %s\n" $cnt ${chips[$cnt]}
		((cnt+=1))
	done

	while true ; do
		read -p "Choice: " choice
		if [ -z "${choice}" ] ; then
		continue
	fi

	if [ -z "${choice//[0-9]/}" ] ; then
		if [ $choice -ge 0 -a $choice -lt $cnt ] ; then
			export LITOS_CHIP="${chips[$choice]}"
			echo "export LITOS_CHIP=${chips[$choice]}" >> .buildconfig
			break
		fi
	fi
	printf "Invalid input ...\n"
	done
}


function select_app()
{
	local cnt=0
	local choice
	local app_list

	printf "All available apps:\n"
	for appdir in `ls -F applications/ |grep "/$"` ; do
		apps[$cnt]=`basename $appdir`
		printf "%4d. %s\n" $cnt ${apps[$cnt]}
		((cnt+=1))
	done

	while true ; do
		read -p "Choice: " choice
		if [ -z "${choice}" ] ; then
		continue
	fi

	if [ -z "${choice//[0-9]/}" ] ; then
		if [ $choice -ge 0 -a $choice -lt $cnt ] ; then
			export LITOS_APP="${apps[$choice]}"
			echo "export LITOS_APP=${apps[$choice]}" >> .buildconfig
			break
		fi
	fi
	printf "Invalid input ...\n"
	done
}

function select_board()
{
    local cnt=0
    local choice
    local board_list

    printf "All available board:\n"
    for boarddir in `ls -F litos/chips/${LITOS_CHIP}/boards |grep "/$"` ; do
        boards[$cnt]=`basename $boarddir`
        printf "%4d. %s\n" $cnt ${boards[$cnt]}
        ((cnt+=1))
    done
    
    while true ; do
        read -p "Choice: " choice
        if [ -z "${choice}" ] ; then
            continue
        fi  

        if [ -z "${choice//[0-9]/}" ] ; then
            if [ $choice -ge 0 -a $choice -lt $cnt ] ; then
                export LITOS_BOARD="${boards[$choice]}"
		echo "export LITOS_BOARD=${boards[$choice]}" >> .buildconfig
                break
            fi  
        fi  
        printf "Invalid input ...\n"
    done
}

if [ "x$1" = "xconfig" ] ;then
	rm -f .buildconfig
	#select_app $?
	select_chip $?
	#select_board $?
elif [ "x$1" = "xclean" ] ;then
	rm -f ./build/*
elif [ "x$1" = "xdistclean" ] ;then
	rm -f ./build/*
	rm -f .buildconfig
elif [ "x$1" = "xerase" ] ;then
	source .buildconfig	
	rm ${LITOS_PROJECT_DIR}/openocd.cfg
	touch ${LITOS_PROJECT_DIR}/openocd.cfg
	echo "set _HEXFILE ${LITOS_PROJECT_DIR}/build/${LITOS_APP}.hex" >> ${LITOS_PROJECT_DIR}/openocd.cfg
	echo "interface cmsis-dap" >> ${LITOS_PROJECT_DIR}/openocd.cfg
	echo "source [find target/stm32f4x.cfg]" >> ${LITOS_PROJECT_DIR}/openocd.cfg
	
	openocd -f ./openocd.cfg -c 'init' -c 'reset halt' -c 'program $_HEXFILE' -c 'reset' -c 'exit'
elif [ $# -eq 0 ] ; then
	source .buildconfig

	if [ ! -n "$LITOS_CHIP" ] ;then
		echo "run './build.sh config' select chip first please!"
		exit 1
	else
		echo "build for chip:$LITOS_CHIP"
	fi

	rm -rf ./build
	mkdir -p build/

	source ${LITOS_PROJECT_DIR}/bsp/${LITOS_CHIP}/headers.sh
	source ${LITOS_PROJECT_DIR}/bl/headers.sh

	source ${LITOS_PROJECT_DIR}/build_flag.sh

	echo "------"
	echo "CFLAGS = ${CFLAGS}"
	echo "------"
	echo "LDFLAGS = ${LDFLAGS}"
	echo "------"
	echo "CHIP_INCLUDES = ${CHIP_INCLUDES}"
	echo "------"
	echo "APP_INCLUDES = ${APP_INCLUDES}"
	echo "------"
	
	source ${LITOS_PROJECT_DIR}/bsp/${LITOS_CHIP}/sources.sh
	source ${LITOS_PROJECT_DIR}/bl/sources.sh

	echo "linking ............"

    ${CC} ${LDFLAGS} ${LIBS} -o ${LITOS_PROJECT_DIR}/build/${LITOS_APP}.elf  ${LITOS_PROJECT_DIR}/build/*.o
	${SZ} ${LITOS_PROJECT_DIR}/build/${LITOS_APP}.elf
	${CP} -O ihex ${LITOS_PROJECT_DIR}/build/${LITOS_APP}.elf ${LITOS_PROJECT_DIR}/build/${LITOS_APP}.hex

	echo "building completed"
fi
